package handler;

import beans.Plan;
import beans.Task;
import beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.supervisorService;

import java.util.List;


@Controller
@RequestMapping("/super")
public class supervisorHandler {

    @Autowired
    private supervisorService supervisor;

    @RequestMapping("/viewTask")
    public String viewTask(Integer userId, Model model){
        List<Task> tasks=supervisor.selectTaskByUserId(userId);
        model.addAttribute("tasks",tasks);

        return "supervisor/view_tasks";
    }

    @RequestMapping("/openDetailPlan")
    public String openDetailPlan(Integer planId,String taskName,Model model){
        Plan plan=supervisor.selectPlanById(planId);
        model.addAttribute("plan",plan);
        model.addAttribute("taskName",taskName);
        return "supervisor/detail_plan";
    }

    @RequestMapping("/openNewTask")
    public String openNewTask(Integer userId,Model model){
        List<User> users=supervisor.selectEmpByUserId(userId);
        model.addAttribute("users",users);
        return "supervisor/develop_task";
    }

    @RequestMapping("/addTask")
    public String addTask(Task task,Model model){
        supervisor.addTask(task);
        return this.viewTask(task.getSuper_id(),model);
    }

    @RequestMapping("/openAdjustTask")
    public String openAdjustTask(Integer userId,Model model){
        List<Task> tasks=supervisor.selectTaskByUserId(userId);
        model.addAttribute("tasks",tasks);
        return "supervisor/adjusting_tasks";
    }

    @RequestMapping("/openAdjustTaskDetail")
    public String openAdjustTaskDetail(Integer taskId,Integer userId,Model model){
        Task task=supervisor.selectTaskById(taskId);
        List<User> users=supervisor.selectEmpByUserId(userId);
        model.addAttribute("task",task);
        model.addAttribute("users",users);
        return "supervisor/adjust_tasks_detail";
    }

    @RequestMapping("/updateTaskById")
    public String updateTaskById(Task task,Model model){
        supervisor.updateTaskById(task);
        return this.viewTask(task.getSuper_id(),model);
    }

    @RequestMapping("/openDetailTask")
    public String openDetailTask(Integer taskId,Model model){
        Task task=supervisor.selectTaskById(taskId);
        List<Plan> plans=supervisor.selectPlanByTaskId(taskId);
        model.addAttribute("task",task);
        model.addAttribute("plans",plans);
        return "supervisor/detail_task";
    }

    @RequestMapping("/openTrackTask")
    public String openTrackTask(Integer userId,Model model){
        List<Task> tasks=supervisor.selectTaskByUserId(userId);
        model.addAttribute("tasks",tasks);
        return "supervisor/tracking_tasks";
    }

    @RequestMapping("/openEmp")
    public String openEmp(Integer userId,Model model){
        List<User> users=supervisor.selectEmpByUserId(userId);
        model.addAttribute("users",users);
        return "supervisor/view_employee";
    }

    @RequestMapping("/userDetail")
    public String userDetail(Integer empId, Model model){
        User user=supervisor.selectEmpById(empId);
        model.addAttribute("user",user);
        return "admin/user_information";
    }

}
